package com.toogu.mazhangmen.ui.monitor;

import android.app.Activity;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.google.gson.Gson;
import com.toogu.mazhangmen.R;
import com.toogu.mazhangmen.bean.EquipmentsBean;
import com.toogu.mazhangmen.ui.view.MonitorView;
import com.toogu.mazhangmen.ui.view.NavitationLayout;
import com.toogu.mazhangmen.util.MonitorUtil;

import java.util.ArrayList;
import java.util.List;

public class MonitorIView implements MonitorContract.IView{

    private Activity activity = null;
    private Delegate delegate = null;


    private TextView title;
    private MonitorView monitorView;

    private NavitationLayout navitationLayout;
    private ViewPager viewPager;
    private List<Fragment> fragmentList;
    private ViewPagerAdapter viewPagerAdapter;


    private RecyclerView recyclerView;
    private List<EquipmentsBean> equipmentsBeanList;
    private MonitorAdapter monitorAdapter;



    public MonitorIView(Activity activity){
        this.activity = activity;
    }

    @Override
    public void onCreate() {
        EquipmentsBean  equipmentsBean = new Gson().fromJson(activity.getIntent().getStringExtra("equipmentsBean"), EquipmentsBean.class);
        title=activity.findViewById(R.id.title);

        monitorView=activity.findViewById(R.id.monitorView);
        monitorView.setConnecting();

        recyclerView=activity.findViewById(R.id.recyclerView);
        equipmentsBeanList = new ArrayList<>();
        monitorAdapter = new MonitorAdapter(activity, equipmentsBeanList, equipmentsBean.getCode(), delegate.getOnChoiceChange());
        recyclerView.setLayoutManager(new LinearLayoutManager(activity, RecyclerView.HORIZONTAL, false));
        recyclerView.setAdapter(monitorAdapter);

        viewPager=activity.findViewById(R.id.viewPager);
        fragmentList=new ArrayList<>();
        viewPagerAdapter=new ViewPagerAdapter(((AppCompatActivity)activity).getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.setCurrentItem(0);

        navitationLayout=activity.findViewById(R.id.navitationLayout);
        navitationLayout.setViewPager(activity, new String[]{"控制面板","设备信息"}, viewPager, R.color.white, R.color.text_blue, 16, 16, 0, 0, true);


    }

    @Override
    public void onResume() {
    }

    @Override
    public void onPause() {

    }

    @Override
    public void onDestory() {
        fragmentList.clear();
    }

    @Override
    public void showDate(List<EquipmentsBean> equipmentsBeanList) {
        this.equipmentsBeanList.addAll(equipmentsBeanList);
        monitorAdapter.notifyDataSetChange();
    }

    @Override
    public void setTitle(String title) {
        this.title.setText(title);
    }

    @Override
    public void showEquipment(EquipmentsBean equipmentsBean) {
        MonitorUtil monitorUtil =  delegate.setMonitorUtil(equipmentsBean,monitorView);
        fragmentList.clear();
        fragmentList.add(new ControlFragment(equipmentsBean,monitorUtil));
        fragmentList.add(new MessageFragment(equipmentsBean));
        viewPagerAdapter.notifyDataSetChanged();

        monitorAdapter.notifyDataSetChange();
    }

    @Override
    public void setViewDelegate(Delegate delegate) {
        this.delegate=delegate;
    }


}
